package com.baizhi.controller;

import com.baizhi.entity.Product;
import com.baizhi.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("add")
    public Map addProduct(Product product){
        Map map = new HashMap();
        try {
            productService.insert(product);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map removeProduct(Integer pid){
        Map map = new HashMap();
        try {
            productService.delete(pid);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("removeAny")
    public Map removeAnyProduct(Integer[] pids){
        Map map = new HashMap();
        try {
            productService.deleteAny(pids);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    public Map updateAnyProduct(Product product){
        Map map = new HashMap();
        try {
            productService.update(product);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Product getOneProduct(Integer pid){
        return productService.selectById(pid);
    }
    @RequestMapping("show")
    public Map showProduct(String pname,Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Product> pageInfo = productService.selectByPage(pname, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
}
